<?php

namespace app\api\controller;

use app\common\library\wechat\WxPay;
use app\store\model\Order as SOrder;
use think\Log;

/**
 * 支付成功异步通知接口
 * Class Notify
 * @package app\api\controller
 */
class Notify
{
    /**
     * 支付成功异步通知
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function order()
    {
        // 微信支付组件：验证异步通知
        $WxPay = new WxPay();
        $WxPay->notify();
    }

    /**
     * 顺丰api回调
     * @return void
     *
     * opCode=50 揽货
     * opCode=70 取消
     */
    public function sfCallback()
    {
        $json = file_get_contents('php://input');
        Log::write('收到回调数据：'.$json, 'sf-info');
        $arr = json_decode($json, true);
        if (empty($arr)){
            echo 'FAIL'; exit();
        }
        // 只处理揽货
        if (empty($arr['opCode']) || $arr['opCode'] != '54'){
            echo 'FAIL2'; exit();
        }
        // 处理订单发货
        $order = \app\common\model\Order::where('express_no', $arr['mailNo'])->find();
        if (!$order){
            Log::write('订单不存在：' . $arr['mailNo'], 'sf-info');
        }

        // 修改发货信息
        $order_m = SOrder::detail($order['order_id']);
        $express = ['express_id'=>10001, 'express_no'=>$arr['mailNo']];
        if ($order_m->delivery($express))
        {
            Log::write('发货成功 order_no='.$order['order_no'], 'sf-info');
        }else{
            Log::write('发货失败：' . $order_m->getError(), 'sf-info');
        }

        echo 'SUCCESS'; exit();
    }
}
